package com.ruoyi.util.security;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class SandboxUtil {
    public static final Logger logger = LoggerFactory.getLogger(SandboxUtil.class);

    /**
     * 提交检测分析任务
     * @param host
     * @param token
     * @param file
     * @param os
     */
    public static SandboxResult submitTask(String host, String token, File file, String os) {
        Map<String, Object> filename = new HashMap<>();
        filename.put("is_public", true);
        filename.put("active_reboot", false);

        Map<String, Object> config_map = new HashMap<>();
        config_map.put("os", os); // 目标平台
        config_map.put("strategy", 0); // 自动快速检测
        config_map.put(file.getName(), filename);

        Map<String, Object> param = new HashMap<>();
        param.put("category", "file"); // 投递类型，投递文件任务时指定为 file
        param.put("file_list", file);
        param.put("config_map", JSON.toJSONString(config_map));

        String path = host + "/api/v2/task/submit";

        logger.info("------submitTask-path: {}", path);
        logger.info("------submitTask-param: {}", param);

        String text = HttpRequest.post(path)
                .header("Authorization", "Token " + token)
                .form(param)
                .execute().body();

        logger.info("------submitTask-result: {}", text);

        SandboxResult result = JSON.parseObject(text, SandboxResult.class);
        return result;

    }

    /**
     * 查看任务报告内容
     * @param host
     * @param token
     * @param taskId
     */
    public static SandboxResult reportOverview(String host, String token, String taskId) {
        String path = host + "/api/v2/task/report/overview/" + taskId;

        logger.info("-----请求地址：{}", path);
        String text = HttpRequest.get(path)
                .header("Authorization", "Token " + token)
                .execute().body();
        SandboxResult result = JSON.parseObject(text, SandboxResult.class);
        return result;
    }

    /**
     * 信息标签
     * @param host
     * @param token
     * @param taskId
     * @return
     */
    public static SandboxResult reportTags(String host, String token, String taskId) {
        String path = host + "/api/v1/task/report/tags/" + taskId;
        String text = HttpRequest.get(path)
                .header("Authorization", "Token " + token)
                .execute().body();
        SandboxResult result = JSON.parseObject(text, SandboxResult.class);
        return result;
    }

    /**
     * 运行截图
     * @param host
     * @param token
     * @param taskId
     * @return
     */
    public static SandboxResult reportScreenshot(String host, String token, String taskId) {
        String path = host + "/api/v1/task/report/shots/" + taskId;
        String text = HttpRequest.get(path)
                .header("Authorization", "Token " + token)
                .execute().body();
        SandboxResult result = JSON.parseObject(text, SandboxResult.class);
        return result;
    }

    public static SandboxResult sandboxNetwork(String host, String token, String taskId, Map<String, Object> params) {
        String path = host + "/api/v1/task/report/network/" + taskId;
        logger.info("-----请求地址：{}\n{}", path, params);

        String text = HttpRequest.get(path)
                .header("Authorization", "Token " + token)
                .form(params)
                .execute().body();
        SandboxResult result = JSON.parseObject(text, SandboxResult.class);
        return result;


    }

    public static SandboxResult sandboxBehaviorProcess(String host, String token, String taskId, Map<String, Object> params) {
        String path = host + "/api/v1/task/report/behavior/process/" + taskId;
        logger.info("-----请求地址：{}\n{}", path, params);

        String text = HttpRequest.get(path)
                .header("Authorization", "Token " + token)
                .form(params)
                .execute().body();
        SandboxResult result = JSON.parseObject(text, SandboxResult.class);
        return result;
    }

    public static SandboxResult sandboxBehaviorEventsByPid(String host, String token, String taskId, String pid, String eventType, Map<String, Object> params) {
        String path = host + "/api/v1/task/report/behavior/events/" + taskId + "/" + pid + "/" + eventType;
        logger.info("-----请求地址：{}\n{}", path, params);

        String text = HttpRequest.get(path)
                .header("Authorization", "Token " + token)
                .form(params)
                .execute().body();
        SandboxResult result = JSON.parseObject(text, SandboxResult.class);
        return result;
    }

    public static SandboxResult sandboxReportStatic(String host, String token, String taskId, Map<String, Object> params) {
        String path = host + "/api/v1/task/report/static/" + taskId;
        logger.info("-----请求地址：{}\n{}", path, params);

        String text = HttpRequest.get(path)
                .header("Authorization", "Token " + token)
                .form(params)
                .execute().body();
        SandboxResult result = JSON.parseObject(text, SandboxResult.class);
        return result;
    }
}
